// 这是一个模拟的图像检测服务
// 在实际应用中，您需要集成真实的图像识别 API，如 Google Cloud Vision, AWS Rekognition 等

export interface DetectionObject {
  id: string
  name: string
  confidence: number
  type: "person" | "object" | "scene"
}

export interface DetectionResult {
  id: string
  imageUrl: string
  objects: DetectionObject[]
  timestamp: number
  fileSize: string
  resolution: string
  processingTime: number
}

// 获取本地存储的历史记录
export async function getDetectionHistory(): Promise<DetectionResult[]> {
  // 模拟 API 延迟
  await new Promise((resolve) => setTimeout(resolve, 500))

  if (typeof window !== "undefined") {
    try {
      const data = localStorage.getItem("photo-detect-history")
      if (data) {
        const parsed = JSON.parse(data)
        if (Array.isArray(parsed)) {
          return parsed
        }
      }
    } catch (error) {
      console.error("Failed to get history from localStorage:", error)
    }
  }

  return []
}

// 获取检测结果
export async function getDetectionResult(id: string): Promise<DetectionResult | null> {
  // 模拟 API 延迟
  await new Promise((resolve) => setTimeout(resolve, 300))

  if (typeof window !== "undefined") {
    try {
      const data = localStorage.getItem("photo-detect-history")
      if (data) {
        const history = JSON.parse(data)
        if (Array.isArray(history)) {
          return history.find((result) => result.id === id) || null
        }
      }
    } catch (error) {
      console.error("Failed to get result from localStorage:", error)
    }
  }

  return null
}

// 模拟图像检测过程
export async function mockDetectImage(file: File): Promise<DetectionResult> {
  // 模拟处理延迟
  await new Promise((resolve) => setTimeout(resolve, 1500))

  // 创建一个 URL 用于预览
  const imageUrl = URL.createObjectURL(file)

  // 生成随机对象检测结果
  const possibleObjects = [
    { name: "人", type: "person" },
    { name: "汽车", type: "object" },
    { name: "建筑", type: "object" },
    { name: "树", type: "object" },
    { name: "狗", type: "object" },
    { name: "猫", type: "object" },
    { name: "桌子", type: "object" },
    { name: "椅子", type: "object" },
    { name: "手机", type: "object" },
    { name: "电脑", type: "object" },
    { name: "城市", type: "scene" },
    { name: "海滩", type: "scene" },
    { name: "山", type: "scene" },
    { name: "室内", type: "scene" },
    { name: "户外", type: "scene" },
  ]

  // 随机选择 3-8 个对象
  const numObjects = Math.floor(Math.random() * 6) + 3
  const selectedObjects: DetectionObject[] = []

  for (let i = 0; i < numObjects; i++) {
    const randomIndex = Math.floor(Math.random() * possibleObjects.length)
    const obj = possibleObjects[randomIndex]

    selectedObjects.push({
      id: `obj-${Date.now()}-${i}`,
      name: obj.name,
      confidence: Math.random() * 0.3 + 0.7, // 0.7 - 1.0
      type: obj.type as "person" | "object" | "scene",
    })

    // 避免重复
    possibleObjects.splice(randomIndex, 1)
    if (possibleObjects.length === 0) break
  }

  // 创建结果对象
  const result: DetectionResult = {
    id: `result-${Date.now()}`,
    imageUrl,
    objects: selectedObjects,
    timestamp: Date.now(),
    fileSize: `${(file.size / (1024 * 1024)).toFixed(2)} MB`,
    resolution: "未知", // 在实际应用中，您可以从图像中获取这些信息
    processingTime: Number.parseFloat((Math.random() * 2 + 0.5).toFixed(1)),
  }

  return result
}
